package com.sdk.demo;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.annotation.TargetApi;
import android.app.Service;
import android.content.Intent;
import android.graphics.Path;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import java.util.List;

// com.sdk.demo.TouchServer
public class TouchServer extends AccessibilityService {
    private String TAG = "eve";
    public void onCreate(){
       super.onCreate();
  //      AutoTouch autoTouch = new AutoTouch();
    ///    autoTouch.autoClickPos(100,100,200,200);
        //this.dispatchGesture();

    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        int eventType = event.getEventType();
        String className = event.getClassName().toString();
        Log.e(TAG," eventType is "+eventType+" class name is "+className);
        int type = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
        if(className.equals("com.tcm.doctor.MainActivity")){
            AccessibilityNodeInfo nodeInfo =event.getSource();
          //  nodeInfo = getRootInActiveWindow();
            if (nodeInfo != null) {
               int count =  nodeInfo.getChildCount();
                Log.e(TAG," nodeInfo.getChildCount() is "+ count);
                for(int i = 0;i < count;i++){
                    AccessibilityNodeInfo childInfo = nodeInfo.getChild(i);
                    Log.e(TAG," childInfo is "+childInfo.getViewIdResourceName());
                    String id = childInfo.getViewIdResourceName();
                    if(id.contains("id/listPatient")){
                        Log.e(TAG," id/listPatient");
                    }
                }
                List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByViewId("id/listPatient");
                Log.e(TAG," list is "+list);
            }
        }else if(className.equals("android.support.v7.widget.RecyclerView")){
            AccessibilityNodeInfo nodeInfo1 =  event.getSource();
         //   Log.e(TAG," nodeInfo1 is "+nodeInfo1.getViewIdResourceName());
        //    nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
         //   nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
        //    nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
          //  nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
        }
       // this.dispatchGesture()
        topGestureClick();

      //  click();

    }

    int index = 0;
    private void click(){
        int width = getResources().getDisplayMetrics().widthPixels;
        int h = getResources().getDisplayMetrics().heightPixels;
        int height = 300;
        for(int index = 0;index < 30;index++){
            int x = width - (200);
            int y = height + (index * 20);
            Log.e("in"," x is "+x+" y is "+y+" index is "+index);
            if(x <= 0 || y >= h){
                index = 0;
                break;
            }
            topGestureClick(x,height + (index * 20));
        }

    }

    @Override
    public void onInterrupt() {

    }

    public class AutoTouch {

        public void autoClickPos(final double x1, final double y1,final double x2, final double y2){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while(true){
                        try{
                            Thread.sleep(500);
                        }catch (Exception e){
                            e.printStackTrace();
                        }

                        //    String[] order = {"input", "swipe", "" + x1, "" + y1, "" + x2, "" + y2,};
                        String[] order = {"input", "keyevent", "4"};
                        try{
                            new ProcessBuilder(order).start();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
        }
    }
    @TargetApi(Build.VERSION_CODES.O)
    private void topGestureClick() {
        GestureDescription.Builder builder = new GestureDescription.Builder();
        Path path = new Path();
        int y = 500;
        int x = 360;
        path.moveTo(360, y);
        path.lineTo(x += 3, y += 2500);
         GestureDescription gestureDescription = builder.addStroke(new GestureDescription.StrokeDescription(path, 200L, 500L,true)).build();
         dispatchGesture(gestureDescription, new AccessibilityService.GestureResultCallback() {
             @Override
             public void onCompleted(GestureDescription gestureDescription) {
                 super.onCompleted(gestureDescription);
                 Log.e(TAG, "123===onCompleted" );
                 /*
                 handler.postDelayed(new Runnable() {
                     @Override
                     public void run() {
                         leftGestureClick();
                     }
                     },3000);*/
                click();
             }
             @Override
             public void onCancelled(GestureDescription gestureDescription) {
                 Log.e(TAG, "123===onCancelled" );
                 click();
             }
             }, new Handler(Looper.getMainLooper()));

    }

    @TargetApi(Build.VERSION_CODES.O)
    private void topGestureClick(int x,int y) {
        GestureDescription.Builder builder = new GestureDescription.Builder();
        Path path = new Path();
        path.moveTo(x, y);
        GestureDescription gestureDescription = builder.addStroke(new GestureDescription.StrokeDescription(path, 800L, 800L,false)).build();
        dispatchGesture(gestureDescription, new AccessibilityService.GestureResultCallback() {
            @Override
            public void onCompleted(GestureDescription gestureDescription) {
                super.onCompleted(gestureDescription);
                Log.e(TAG, "123 === 111onCompleted index is "+ index );
                 /*
                 handler.postDelayed(new Runnable() {
                     @Override
                     public void run() {
                         leftGestureClick();
                     }
                     },3000);*/
            }
            @Override
            public void onCancelled(GestureDescription gestureDescription) {
                Log.e(TAG, "123===111onCancelled index is "+index );
            }
        }, new Handler(Looper.getMainLooper()));

    }
}
